
const mysql = require('mysql');
const connection = mysql.createConnection({
    host: 'localhost',// 数据库地址
    user: 'root',// 数据库用户
    password: '123',// 数据库密码
    database: 'wozijiname'// 要连接的数据库
});

function zhaohui(sql,a) {
    let aa= a[1];
    return new Promise((resolve, reject) => {
        connection.query('select * from zhanghu where telphone=?', aa,(error, results, fields) => {
            if(results[0]){
                resolve(chazhao(sql,a))
                // connection.end();
            }else {
                let postData = {code: '100'};
                resolve(postData);
                // connection.end();
            }
        });
    });
}
function zhuce(sql,a) {
    let aa= a[0];
    return new Promise((resolve, reject) => {
        connection.query('select * from zhanghu where telphone=?', aa,(error, results, fields) => {
            if(results[0]){
                let postData = {code: '100'};
                resolve(postData);
                // connection.end;
            }else {
                resolve(chazhao(sql,a))
                // connection.end;
            }
        });
    });
}
function chazhao(sql,a) {
    return new Promise((resolve, reject) => {
        connection.query(sql, a,(error, results, fields) => {
            var postData={code: '10',data:results};
            if(error){
                console.log(error);
                resolve(postData);
            }else {
            if(results[0]){
                postData = {code: '200',data:results};
                resolve(postData);
                // connection.end();
            }else if(results){
                postData = {code: '300',data:results};
                resolve(postData);
                // connection.end();
            }else {
                postData = {code: '0',data:results};
                resolve(postData);
                // connection.end();
            }}
        });
    });
}
function chaxun(sql,a) {
    return new Promise((resolve, reject) => {
        connection.query(sql, a,(error, results, fields) => {
            if(error){
                console.log(error);
                resolve(error);
            }else {
                resolve(results);
            }
        });
    });
}
module.exports = {connection,zhaohui,zhuce,chazhao,chaxun}

